package com.smart.builder.entity;

import java.math.BigDecimal;
import java.util.Map;

import javax.persistence.EntityManager;

import com.smart.persistence.entity.Hotel;
import com.smart.persistence.entity.RoomModel;
import com.smart.persistence.service.SmartBasicCrudDAO;
import com.smart.service.infohotel.InfoWebHotelService;


public class BuilderEntityRoomModel {
	
	private RoomModel roomT = null;
	private int codeRoom =0;
	
	public BuilderEntityRoomModel(int codeRoom, EntityManager em){
		super();
		this.codeRoom = codeRoom;
		find(em);
		if(roomT == null){
			roomT = new RoomModel();
			roomT.setCodRoom(codeRoom);
		}
	}


	public BuilderEntityRoomModel setHotel(Hotel hotel) {
		assert hotel.getId() != 0: "Hotel id = 0";
		roomT.setHotel(hotel);
		return this;
	}
	
	
	public BuilderEntityRoomModel save(EntityManager em) {
		if(roomT.getId() != 0){
			em.merge(roomT);
		}else{
			em.persist(roomT);
		}
		find(em);
		return this;
	}
	
	private void find(EntityManager em) {
		roomT = SmartBasicCrudDAO.getInstance().consultaQuerySingle(
				em, RoomModel.class,
				RoomModel.findByCodeRoom, codeRoom);
	}
	
	
	public BuilderEntityRoomModel setMap(Map<String, String> map){
		addInfoDescription(map);
		addInfoDetails(map);
		addInfoImage(map);
		roomT.setInfo(map.get(InfoWebHotelService._info));
		addInfoName(map);
		addInfoPrice(map);
		addInfoNumberOfAdults(map);
		addInfoNumberOfChildren(map);
		return this;
	}


	private void addInfoNumberOfChildren(Map<String, String> map) {
		String qtiC = map.get(InfoWebHotelService._numberOfChildren);
		if(qtiC.matches("\\d+")){
			roomT.setMaxAmountChildren(new Integer(qtiC));	
		}else{
			roomT.setMaxAmountChildren(4);
		}
		
	}


	private void addInfoNumberOfAdults(Map<String, String> map) {
		String qtiC = map.get(InfoWebHotelService._numberOfAdults);
		if(qtiC.matches("\\d+")){
			roomT.setMaxAmountAdults(new Integer(qtiC));
		}else{
			roomT.setMaxAmountAdults(2);
		}
	}


	private void addInfoImage(Map<String, String> map) {
		roomT.setImage(map.get(InfoWebHotelService._hotelImageURL));
	}


	private void addInfoName(Map<String, String> map) {
		roomT.setName(map.get(InfoWebHotelService._roomTypeName));
	}


	private void addInfoPrice(Map<String, String> map) {
		try{
			roomT.setPrice(new BigDecimal(map.get(InfoWebHotelService._preco)));	
		}catch(NumberFormatException e){
			e.printStackTrace();
			roomT.setPrice(new BigDecimal(100));
		}
		
	}




	private void addInfoDescription(Map<String, String> map) {
		String description = map.get(InfoWebHotelService._descricao);
		if (description != null && description.length() > 299) {
			description = description.substring(0, 299);
		}
		roomT.setDescription(description);
	}


	private void addInfoDetails(Map<String, String> map) {
		String details = map.get(InfoWebHotelService._detalhes);
		if (details != null && details.length() > 499) {
			details = details.substring(0, 499);
		}
		roomT.setDetails(details);
	}
	
	public RoomModel getRoomT() {
		return roomT;
	}

}
